package ltd.bugs.cute.common.spring;

import java.util.Properties;

/**
 * 系统的所有配置信息均从这边获取
 * 
 * @author zhengzhq
 *
 */
public class ApplicationConfiguration {

  private static Properties applicationProperties;

  public static void setProperties(Properties cuteProperties) {
    applicationProperties = cuteProperties;
  }

  @SuppressWarnings("unchecked")
  public static <T> T getConfig(String key, Class<T> type) {
    if (!applicationProperties.containsKey(key)) {
      return null;
    }

    Object value = applicationProperties.get(key);
    if (type == Boolean.class) {
      return (T) new Boolean(value.toString());
    }

    if (type == Integer.class) {
      return (T) new Integer(value.toString());
    }

    if (type == Double.class) {
      return (T) new Double(value.toString());
    }

    if (type == Float.class) {
      return (T) new Float(value.toString());
    }
    return (T) value;
  }

  public static <T> T getConfig(String key, Class<T> type, T defaultVal) {
    T value = getConfig(key, type);
    if (value == null) {
      value = defaultVal;
    }

    return value;
  }
}
